package com.example.administrator.baseadapter.recyclerview.holder;

import android.content.Context;
import android.content.res.Resources;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.baseadapter.recyclerview.image.ImageLoaderHelper;


/**
 * 实现View的绑定，实现逻辑就是将View取出后缓存在内存中，方便后面绑定数据,所有界面均可使用
 */
public class ViewHolder implements IViewHolder {

    protected View mConvertView;

    protected Context mContext;

    protected SparseArray<View> mViews;

    public ViewHolder(Context context, View convertView) {
        this.mViews = new SparseArray<View>();
        this.mContext = context;
        this.mConvertView = convertView;
    }

    /**
     * @param context     app context
     * @param convertView container view
     * @return view holder
     */
    public static IViewHolder create(Context context, View convertView) {
        return new ViewHolder(context, convertView);
    }

    @Override
    public View getConvertView() {
        return mConvertView;
    }

    @Override
    public Context getContext() {
        return mContext;
    }

    @Override
    public Resources getResources() {
        if (mContext != null) {
            return mContext.getResources();
        }
        return null;
    }

    /**
     * @param viewId view activity_id
     * @return view
     */
    @Override
    public <T extends View> T getView(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    @Override
    public int getColor(@ColorRes int resId) {
        if (getResources() != null) {
            return getResources().getColor(resId);
        }
        return -1;
    }

    @Override
    public String getString(@StringRes int resId) {
        return getContext().getResources().getString(resId);
    }

    @Override
    public CharSequence getText(@IdRes int viewId) {
        TextView textView = getView(viewId);
        return textView.getText();
    }

    @Override
    public int getVisibility(@IdRes int viewId) {
        return getView(viewId).getVisibility();
    }

    /**
     * @param viewId     view activity_id
     * @param visibility view display state
     * @return view holder
     */
    @Override
    public IViewHolder setVisibility(int viewId, int visibility) {
        View view = getView(viewId);
        if (view != null && view.getVisibility() != visibility) {
            view.setVisibility(visibility);
        }
        return this;
    }

    @Override
    public IViewHolder setEnabled(@IdRes int viewId, boolean enabled) {
        View view = getView(viewId);
        if (view != null) {
            view.setEnabled(enabled);
        }
        return this;
    }

    @Override
    public IViewHolder setCheckBox(@IdRes int viewId, boolean checked) {
        View view = getView(viewId);
        if (view != null && view instanceof CheckBox) {
            ((CheckBox) view).setChecked(checked);
        }
        return this;
    }

    @Override
    public IViewHolder setText(@IdRes int viewId, @StringRes int resId) {
        return setText(viewId, getString(resId));
    }

    @Override
    public IViewHolder setText(@IdRes int viewId, CharSequence text) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setText(text);
        }
        return this;
    }

    @Override
    public IViewHolder setTextColor(int viewId, @ColorInt int textColor) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setTextColor(textColor);
        }
        return this;
    }

    @Override
    public IViewHolder setTextColorRes(int viewId, @ColorRes int resId) {
        TextView view = getView(viewId);
        if (view != null) {
            view.setTextColor(getColor(resId));
        }
        return this;
    }

    @Override
    public IViewHolder setHint(@IdRes int viewId, @StringRes int resId) {
        return setHint(viewId, getString(resId));
    }

    @Override
    public IViewHolder setHint(@IdRes int viewId, CharSequence text) {
        View view = getView(viewId);
        if (view != null && view instanceof TextView) {
            ((TextView) view).setHint(text);
        }
        return this;
    }

    @Override
    public IViewHolder setOnClickListener(int viewId, View.OnClickListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnClickListener(listener);
        }
        return this;
    }

    @Override
    public IViewHolder setOnClickListener(int viewId, Object tag, View.OnClickListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setTag(tag);
            view.setOnClickListener(listener);
        }
        return this;
    }

    @Override
    public IViewHolder setOnTouchListener(int viewId, View.OnTouchListener listener) {
        View view = getView(viewId);
        if (view != null) {
            view.setOnTouchListener(listener);
        }
        return this;
    }

    @Override
    public IViewHolder addTextChangedListener(int viewId, TextWatcher listener) {
        View view = getView(viewId);
        if (view != null && view instanceof TextView) {
            ((TextView) view).addTextChangedListener(listener);
        }
        return this;
    }

    @Override
    public IViewHolder setBackgroundResource(@IdRes int viewId, @DrawableRes int drawable) {
        View view = getView(viewId);
        if (view != null) {
            view.setBackgroundResource(drawable);
        }
        return this;
    }

    @Override
    public IViewHolder setImageResource(@IdRes int viewId, @DrawableRes int drawableId) {
        ImageView view = getView(viewId);
        if (view != null) {
            view.setImageResource(drawableId);
        }
        return this;
    }

    @Override
    public IViewHolder display(Context context, int viewId, String url, @DrawableRes int shouldDrawable) {
        View view = getView(viewId);
        if (view != null && view instanceof ImageView) {
            ImageLoaderHelper.display(context, url, (ImageView) view, shouldDrawable);
        }
        return this;
    }

    @Override
    public IViewHolder recycle() {
        mViews.clear();
        mViews = null;
        mConvertView = null;
        mContext = null;
        return this;
    }

}